package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;
import com.example.fw.ContactObject;
import com.example.fw.Groups;

public class ContactDataGenerator extends TestBase{

static Random rnd = new Random();
public static String contactsFileName = "contact.txt";
	
	@DataProvider(name = "randomContacts")
	public static Iterator<Object[]> generateRandomContacts() throws FileNotFoundException, IOException {
		List<ContactObject> contacts = new ContactDataGenerator().generateRandomContacts(2);
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactObject contact : contacts) {
			list.add(new Object[]{contact});
		}
	return list.iterator();
	}
	
	@DataProvider(name = "contactsFromFile")
	public static Iterator<Object[]> loadContactsFromFile() throws IOException {
		List<Object[]> list = new ArrayList<Object[]>();
		BufferedReader Reader = new BufferedReader(new FileReader(contactsFileName));
		String line = Reader.readLine();
		while (line != null) {
			String[] parts = line.split("\t");
			ContactObject contact = new ContactObject()
			.setFirstname(parts[0])
			.setLastname(parts[1])
			.setAddress(parts[2])
			.setEmail(parts[3])
			.setEmail2(parts[4])
			.setAddress2(parts[5]);
			list.add(new Object[]{contact});
			line = Reader.readLine();
		}
		return list.iterator();
	
	}
	
	public static void main (String[] args){
		//String file = "";
		if (args.length < 2){
			System.out.println("Specify two parameters for contacts generation: file and count");
			return;
		}
		String file = args[0];
		int count = Integer.parseInt(args[1]);
		try {
			new ContactDataGenerator().generateDataToFile(file, count);
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}

	private void generateDataToFile(String fileName, int count) throws IOException {
		List<ContactObject> contacts = generateRandomContacts(count);
		File file = new File(fileName);
		FileWriter writer = new FileWriter(file);
		for (ContactObject contact : contacts) {
			writer.write(contact.firstname + "\t" + contact.lastname + "\t" + contact.address  + "\t" + contact.email + "\t" + contact.email2 + "\t" + contact.address2 + "\r\n");
		}
		writer.close();
		
	}

	private List<ContactObject> generateRandomContacts(int count) throws FileNotFoundException, IOException {
		List<ContactObject> list = new ArrayList<ContactObject>();
		//Groups groupsList = app.getGroupHelper().getGroups();
		ApplicationManager app = ApplicationManager.getInstance();
		Properties props = new Properties();
		props.load(new FileReader("application.properties"));
		app.setProperties(props);
		
		Groups groupsList = app.getGroupHelper().getGroups();
			for (int i = 0; i < count; i++) {
				ContactObject contact = new ContactObject()
				.setFirstname("firstname" + rnd.nextInt())
				.setLastname("lastname" + rnd.nextInt())
				.setAddress("address" + rnd.nextInt())
				.setHometelephone("home" + rnd.nextInt())
				.setMobilelephone("mobile" + rnd.nextInt())
				.setWorktelephone("work" + rnd.nextInt())
				.setEmail(rnd.nextInt() + "e-mail@mail.com")
				.setEmail2(rnd.nextInt() + "e-mail2@mail.com")
				.setBirthday("1")
				.setBirthmonth("February")
				.setBirthyear("1987")
				.setGroup(groupsList.getGroupByIndex(0))
				.setAddress2("address secondary"  + rnd.nextInt())
				.setPhone2("Home secondary" + rnd.nextInt());
				list.add(contact);
			}
		app.stop();
	return list;
	}

}
